/* LockyDB.h - A LockyCode database using ndbm.
 *
 * Copyright (C) 2008 Yuuichi Teranishi
 *
 * This file is part of iSpotter.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * published by the Free Software Foundation.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; If not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#import "LockyDB.h"
#include <stdio.h>
#include <openssl/md5.h>

#define WLDB_ID_SIZE 8

@implementation LockyDB

- (APLocation *)getAPLocation:(NSString *) bssid ssid:(NSString *)ssid signal:(int) signal
{
  unsigned char result[16];
  NSString *str = [bssid stringByAppendingString: ssid];
  NSData *data = [str dataUsingEncoding:NSASCIIStringEncoding];
  unsigned char *inStr = (unsigned char*)[ data bytes ];
  unsigned long len = [data length];
  MD5(inStr, len, result);
//   fprintf (stderr, "Key=%d:%d:%d:%d:%d:%d:%d:%d\n",
// 	   result[0],
// 	   result[1],
// 	   result[2],
// 	   result[3],
// 	   result[4],
// 	   result[5],
// 	   result[6],
// 	   result[7]);
  datum key, fetched;
  key.dptr = result;
  key.dsize = WLDB_ID_SIZE;
  fetched.dptr = NULL;
  fetched = dbm_fetch (dbf, key);
  if (fetched.dptr == NULL) {
    //fprintf (stderr, "no location entry for %s\n", [str cStringUsingEncoding: NSUTF8StringEncoding]) ;
    return NULL;
  }
  else {
//     fprintf (stderr, "LockyCode=%d:%d:%d:%d:%d:%d:%d:%d\n",
// 	     ((unsigned char *)fetched.dptr)[0],
// 	     ((unsigned char *)fetched.dptr)[1],
// 	     ((unsigned char *)fetched.dptr)[2],
// 	     ((unsigned char *)fetched.dptr)[3],
// 	     ((unsigned char *)fetched.dptr)[4],
// 	     ((unsigned char *)fetched.dptr)[5],
// 	     ((unsigned char *)fetched.dptr)[6],
// 	     ((unsigned char *)fetched.dptr)[7]);
    APLocation *loc = [self getAPLocation: ((unsigned char *)fetched.dptr) signal:signal];
    // free(result.dptr);
    return loc;
  }
}

- (APLocation *)getAPLocation:(unsigned char *) lockyCode signal:(int) signal;
{
  unsigned char country = lockyCode[0];
  unsigned char A = lockyCode[1];
  unsigned char B = lockyCode[2];
  unsigned char C = lockyCode[3];
  unsigned char D = lockyCode[4];
  unsigned char E = lockyCode[5];
  unsigned char H = lockyCode[6];
  unsigned char Q = lockyCode[7];
		
  double ax = A % 16;
  double ay = (A - ax) / 16.0;
		
  double bx = B % 16;
  double by = (B - bx) / 16.0;
		
  double cx = C % 16;
  double cy = (C - cx) / 16.0;
		
  double dx = D % 16;
  double dy = (D - dx) / 16.0;
		
  double ex = E % 16;
  double ey = (E - ex) / 16.0;

  return [[APLocation alloc] initWithLat: 20.0 + ay * 2.0 + by * (2.0 / 16.0) + cy * (2.0 / 16.0 / 16.0) + dy * (2.0 / 16.0 / 16.0 / 16.0) + ey * (2.0 / 16.0 / 16.0 / 16.0 / 16.0)
			     lng: 122.0 + ax * 2.0 + bx * (2.0 / 16.0) + cx * (2.0 / 16.0 / 16.0) + dx * (2.0 / 16.0 / 16.0 / 16.0) + ex * (2.0 / 16.0 / 16.0 / 16.0 / 16.0) signal: signal];
}

- (BOOL)open:(NSString *) file
{
  if ((dbf = dbm_open ([file cStringUsingEncoding: NSUTF8StringEncoding],
		       O_RDONLY, 0700)) == NULL) {
    fprintf (stderr, "error opening db\n");
    return NO;
  }
  return YES;
}

- (void)close;
{
  dbm_close(dbf);
}

@end
